/****************************************
Figure A.4, Table A.10, Figure A.5,
Table A.11
*****************************************/

* Load Census Data Cleaned and Collapsed
use "$dir/Data/Final/final_collapsed_ageb_ms.dta", clear
	
gen i = birthstatefip
gen t = (birthdecade/10) + 1 // someone who is 10 when ban is put in place is treated (time 0)
gen Ei = floor(year_of_ban/10) 	// decade when unit is first treated
gen K = t-Ei 			// "relative time" num decades since treated (could be missing if never-treated)


forvalues l = 1/6 {
	gen A`l'event = K==-`l'
}

forvalues l = 0/6 {
	gen P`l'event = K==`l'
}


gen insample6treat = abs(K) <= 6 & K != .



* Interaction treatment vars
forvalues pre = 6(-1)2 {
	cap n gen cmH_Pre`pre' = cmH * A`pre'event
}

forvalues post = 0/6 {
	cap n gen cmH_Post`post' = cmH * P`post'event
}

forvalues x = 0/6 {
	cap label var cmH_Pre`x' "Pre `x'"
	cap label var cmH_Post`x' "Post `x'"
}



replace Ei = . if cmH == 0 /* Treating low CM surnames as never having faced a ban */
gen nevertreated = Ei == . /* this includes low CM surnames and those that never faced a ban */

drop if year_of_ban > 1940 /* this drops all states that banned after 1940 or never banned because they
have year of ban value missing, so control group is low cousin marriage surnames in states that banned
by 1940 */



keep if insample6treat == 1


* There are only very few cells for cmH_post6 for age_bin == 1, so we drop these for the analysis
drop if cmH_Post6 == 1 & age_bin == 1


** Transforming and labelling outcome variables **

gen ln_citypop = ln(citypop)
gen ln_occscore = ln(occscore)

label var birthdecade "Birth decade"
label var cmH ">10% cousin marriage rate (-1858)"
label var birthstatefip "Birth state"
label var resstatefip "Residence state"
label var year "Census year"
label var count_dor "Num census individuals in cell"
label var ln_citypop "Urbanization (log residence pop'n)"
label var ln_occscore "Income (log occscore)"
label var age_bin "Age Bin"
label var nm_single "Never Married/Single"


foreach v of varlist(ln_occscore_nm-ln_citypop_nm){
	label var `v' "Number of individuals with non-missing outcome in cell"
}

compress


/**************************************
Figure A.4
**************************************/

eststo clear

eventstudyinteract ln_citypop   ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if  age_bin == 0, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
	matrix sa_bb = e(b_iw) 
    matrix sa_vb = e(V_iw)
	
	
eventstudyinteract ln_citypop  ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if age_bin == 1, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
	matrix sa_bn = e(b_iw) 
    matrix sa_vn = e(V_iw)
	
	
	event_plot sa_bb#sa_vb  sa_bn#sa_vn, default_look ///
	stub_lag(cmH_Post# cmH_Post#) stub_lead(cmH_Pre# cmH_Pre#) ///
		 plottype(scatter) ciplottype(rcap) ///
	together  trimlead(6) noautolegend ///
	graph_opt(title("", size(medlarge)) ///
		xtitle("Birth decade relative to ban (+1)") ytitle("Urbanization (log residence pop'n)") xlabel(-6(1)6) ylabel(-0.7(0.1)0.7) ///
		legend(order(1 "Age (18-30)" 3 "Age (31-50)") pos(6) rows(3) region(style(none))) ///
	/// the following lines replace default_look with something more elaborate
		xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
	lag_opt1(msymbol(+) color(cranberry)) lag_ci_opt1(color(cranberry)) ///
	lag_opt2(msymbol(Dh) color(black)) lag_ci_opt2(color(black)) 
	//lag_opt3(msymbol(0) color(blue)) lag_ci_opt2(color(blue)) 
graph export "Output/TablesFigures/FigMain_UrbanbyAge.png", replace
	
	
/**************************************
Table A.10
**************************************/

eststo clear

eventstudyinteract ln_citypop   ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if  age_bin == 0, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)

matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum ln_citypop [fw = ln_citypop_nm] if e(sample) == 1
	local obs1 = r(N)
    sum ln_citypop  [aw = ln_citypop_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
 

eventstudyinteract ln_citypop   ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if  age_bin == 1, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)

matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A2
	sum ln_citypop [fw = ln_citypop_nm] if e(sample) == 1
	local obs2 = r(N)
    sum ln_citypop  [aw = ln_citypop_nm] if e(sample) == 1
    local meanB = string(round(r(mean), .01))
 


 estout A1 A2 using "$dir/Output/TablesFigures/results_UrbanbyAge.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' & `obs2' \\"
local tex "`tex' Mean Dep. Var & `meanA' & `meanB' \\"
local tex "`tex' State X Birth Decade FE & Yes & Yes \\"
local tex "`tex' State X High CM F.E. & Yes & Yes \\"
local tex "`tex' Census year F.E. & Yes & Yes \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1  using "$dir/Output/TablesFigures/results_UrbanbyAgea.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" ("2")) ///
	mgroups("Urbanization (log residence pop'n)" "Urbanization (log residence pop'n)", pattern(1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

/**************************************
Figure A.5
**************************************/
	
eststo clear

eventstudyinteract ln_citypop   ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if nm_single == 0 & age_bin == 1, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
	matrix sa_bb = e(b_iw) 
    matrix sa_vb = e(V_iw)
	
	
eventstudyinteract ln_citypop  ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if nm_single == 1 & age_bin == 1, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
	matrix sa_bn = e(b_iw) 
    matrix sa_vn = e(V_iw)
	
	
	event_plot sa_bb#sa_vb  sa_bn#sa_vn, default_look ///
	stub_lag(cmH_Post# cmH_Post#) stub_lead(cmH_Pre# cmH_Pre#) ///
		 plottype(scatter) ciplottype(rcap) ///
	together  trimlead(6) noautolegend ///
	graph_opt(title("", size(medlarge)) ///
		xtitle("Birth decade relative to ban (+1)") ytitle("Urbanization (log residence pop'n)") xlabel(-6(1)6) ylabel(-1.2(0.2)1.2) ///
		legend(order(1 "Married" 3 "Unmarried") position(6) rows(3) region(style(none))) ///
	/// the following lines replace default_look with something more elaborate
		xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
	lag_opt1(msymbol(+) color(cranberry)) lag_ci_opt1(color(cranberry)) ///
	lag_opt2(msymbol(Dh) color(black)) lag_ci_opt2(color(black)) 
	//lag_opt3(msymbol(0) color(blue)) lag_ci_opt2(color(blue)) 
graph export "Output/TablesFigures/FigMain_UrbanMS.png", replace

/**************************************
Table A.11
**************************************/

eststo clear

eventstudyinteract ln_citypop   ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if nm_single == 0 & age_bin == 1, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)

matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum ln_citypop [fw = ln_citypop_nm] if e(sample) == 1
	local obs1 = r(N)
    sum ln_citypop  [aw = ln_citypop_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
 

eventstudyinteract ln_citypop   ///
	cmH ///
    cmH_P* ///
	[aweight =  ln_citypop_nm] if nm_single == 1 & age_bin == 1, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)

matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A2
	sum ln_citypop [fw = ln_citypop_nm] if e(sample) == 1
	local obs2 = r(N)
    sum ln_citypop  [aw = ln_citypop_nm] if e(sample) == 1
    local meanB = string(round(r(mean), .01))
 
 
estout A1 A2 using "$dir/Output/TablesFigures/results_UrbanbyMS.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' & `obs2' \\"
local tex "`tex' Mean Dep. Var & `meanA' & `meanB' \\"
local tex "`tex' State X Birth Decade FE & Yes & Yes \\"
local tex "`tex' State X High CM F.E. & Yes & Yes \\"
local tex "`tex' Census year F.E. & Yes & Yes \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1  using "$dir/Output/TablesFigures/results_UrbanbyMSa.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" ("2")) ///
	mgroups("Urbanization (log residence pop'n)" "Urbanization (log residence pop'n)", pattern(1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

	

